(function () {
    var swap, swapRows;

    swap = function (input1, input2) {
        var tmp;
        tmp = input1.value;
        input1.value = input2.value;
        input2.value = tmp;
    };

    swapRows = function (row1, row2) {
        var inputs1, inputs2, i;
        inputs1 = row1.getElementsByTagName('input');
        inputs2 = row2.getElementsByTagName('input');
        for (i = 3; i--;) {
            swap(inputs1[i], inputs2[i]);
        }
    };
    
    this.addRow = function (anchor) {
        var anchorRow = anchor.parentNode.parentNode, row, cell, input;
        row = this.document.createElement('tr');
        cell = this.document.createElement('td');
        input = this.document.createElement('input');
        input.type = 'text';
        input.name = 'newurl[]';
        cell.appendChild(input);
        row.appendChild(cell);
        cell = this.document.createElement('td');
        input = this.document.createElement('input');
        input.type = 'text';
        input.name = 'newlabel[]';
        cell.appendChild(input);
        row.appendChild(cell);
        cell = this.document.createElement('td');
        input = this.document.createElement('input');
        input.type = 'text';
        input.name = 'newtitle[]';
        cell.appendChild(input);
        row.appendChild(cell);
        anchorRow.parentNode.insertBefore(row, anchorRow);
    };

    this.moveUp = function (anchor) {
        var row, prevRow;
        row = anchor.parentNode.parentNode;
        prevRow = row.previousSibling;
        while (prevRow.nodeType !== 1) {
            prevRow = prevRow.previousSibling;
        }
        swapRows(row, prevRow);
    };

    this.moveDown = function (anchor) {
        var row, nextRow;
        row = anchor.parentNode.parentNode;
        nextRow = row.nextSibling;
        while (nextRow.nodeType !== 1) {
            nextRow = nextRow.nextSibling;
        }
        swapRows(row, nextRow);
    };
})();